﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Windows.Forms;
using CardGamesLibrary.Solitaire;

namespace CardGamesLibrary.UI.Solitaire
{
    public partial class SolitaireTableControl : UserControl
    {
        FrenchDeckPileControl _waste;
        FrenchDeckPileControl _stock;

        SolitaireGameTable _table;

        public SolitaireTableControl()
        {
            InitializeComponent();
        }
        public SolitaireTableControl(SolitaireGameTable table) : this()
        {
            _table = table;
            tableLayoutPanel1.BackColor = Color.Green;
           
            int i = 13;
            foreach (var item in table.TopPiles)
            {
                tableLayoutPanel1.Controls.Add(new FrenchDeckPileControl(item) { Dock = DockStyle.Fill,LastCardOnly= true}, i, 1);
                i -= 2;
            }
            
            i = 13;
            int j = 7;
            foreach (var item in table.Piles)
            {
                
                tableLayoutPanel1.Controls.Add(new FrenchDeckPileControl(item) { Dock = DockStyle.Fill}, i, 3);
                _table.Dealer.DealTo(item, j);
                foreach (var card in item)
                {
                    card.Reverse();
                }
                item.Last().Reverse();
                i -= 2;
                j--;
            }

            _stock = new FrenchDeckPileControl(_table.StockPile) { Dock = DockStyle.Fill, LastCardOnly=true };
            _stock.Click += new EventHandler(_stock_Click);
            tableLayoutPanel1.Controls.Add(_stock, 1, 1);

            _waste = new FrenchDeckPileControl(_table.WastePile) { Dock = DockStyle.Fill, BackColor = Color.Cyan };
            _waste.Click += new EventHandler(_waste_Click);
            tableLayoutPanel1.Controls.Add(_waste, 3, 1);

            _table.Dealer.DealTo(_stock.Pile, 52 -( 7+6+5+4+3+2+1));
            foreach (var item in _stock.Pile)
            {
                if (!item.Reversed)
                    item.Reverse();
            }

        }

        void _waste_Click(object sender, EventArgs e)
        {
            
        }

        void _stock_Click(object sender, EventArgs e)
        {
            if (_stock.Pile.Count > 0)
            {
                var selection = _stock.Pile.Take(3).ToArray();
                foreach (var item in selection)
                {
                    _stock.Pile.Remove(item);
                    item.Reverse();
                    _waste.Pile.Insert(0,item);
                }

            }
            else
            {
                foreach (var item in _waste.Pile.ToArray())
                {
                    _waste.Pile.Remove(item);
                    item.Reverse();
                    _stock.Pile.Insert(0, item);
                }
                
            }
            Refresh();
        }

        private void tableLayoutPanel1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
        {
            e.Action = DragAction.Continue;
        }
    }
}
